package com.xiaouyudeguang.common.message.event;

import com.fasterxml.jackson.databind.JsonNode;
import com.xiaouyudeguang.common.utils.JsonObject;
import lombok.Getter;
import lombok.Setter;
import org.springframework.context.ApplicationEvent;


@Setter
@Getter
public class ChangedEvent extends ApplicationEvent {

    private static final long serialVersionUID = 1L;

    private String id;

    private String appId;

    private String tenantId;

    private String type;

    private String tableName;

    private JsonNode oldSource;

    public ChangedEvent() {
        super(new Object());
    }

    public ChangedEvent(Object source) {
        super(source);
    }

    public JsonNode getJson() {
        return JsonObject.fromJson(source);
    }

    public enum Type {

        INSERT(0),

        UPDATE(1),

        DELETE(-1);

        private final int code;

        Type(int code) {
            this.code = code;
        }

        public int getCode() {
            return code;
        }

        public boolean equals(String name) {
            return name().equals(name);
        }

        public boolean equals(int code) {
            return this.code == code;
        }

        public boolean equals(Type type) {
            return equals(type.code);
        }
    }
}
